//
//  ZMInputImagesTVCell.m
//  FM_Project
//
//  Created by chenzm on 2019/8/9.
//  Copyright © 2019 DL. All rights reserved.
//

#import "ZMInputImagesTVCell.h"

#import "ZMInputImgCVCell.h"
@interface ZMInputImagesTVCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
///

@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;

///
@property(nonatomic,strong)UILabel *titleLbl;

///
@property(nonatomic,strong)NSMutableArray *dataArr;
@end

@implementation ZMInputImagesTVCell{
    CGFloat collect_w;
    CGFloat collect_h;
}

-(void)setModel:(BaseM *)model{
    _model = model;

    self.titleLbl.text = kIsEmptyStr(model.bsTitle1)?@"":model.bsTitle1;
    
    if (model.bsCount == 0) {
        model.bsCount = 9;
    }
    [self.dataArr removeAllObjects];
    for (int i = 0; i<model.bsMarr1.count; i++) {
       BaseM *mm = [BaseM new];
        // 是否显示删除按钮
        mm.bsIsShow1 = model.bsIsShow1;
        // 图片圆角
        mm.bsCornerRaf = model.bsCornerRaf;
        // 此是否添加图片 NO：不添加 YES：添加
        mm.bsIsShow2 = NO;
        mm.bsSelectId = model.bsSelectId;
        mm.bsCount = model.bsCount;
        if (![model.bsMarr1[i] isMemberOfClass:[UIImage class]]) {
            mm.bsImgUrl1 = model.bsMarr1[i];
        } else {
            mm.bsImage = model.bsMarr1[i];
        }
        mm.bsDefImgStr1 = model.bsDefImgStr1;
        [self.dataArr addObject:mm];
    }
    if (model.bsMarr1.count < model.bsCount && model.bsIsAddEdit) {
        BaseM *mm = [BaseM new];
        mm.bsCornerRaf = model.bsCornerRaf;
        mm.bsIsShow1 = NO;
        // 此是否添加图片 NO：不添加 YES：添加
        mm.bsIsShow2 = YES;
        mm.bsSelectId = model.bsSelectId;
        mm.bsDefImgStr1 = model.bsDefImgStr2;
        mm.bsCount = model.bsCount;
        [self.dataArr addObject:mm];
    }
    self.showType = model.bsType;
    [self.collectionView reloadData];
}

-(void)setShowType:(NSInteger)showType{
    _showType = showType;
    switch (showType) {
        case 0:{
            // 默认图片、标题
            self.titleLbl.hidden = NO;
            self.collectionView.hidden = NO;
            self.collectionView.scrollEnabled = NO;
            self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
            self.collectionView.collectionViewLayout = self.flowLayout;
            [_titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(20);
                make.left.mas_equalTo(16);
                make.right.mas_equalTo(-16);
            }];
            
            [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(48);
                make.left.mas_equalTo(16 - 5);
                make.right.mas_equalTo(-16 + 5);
                make.bottom.mas_equalTo(-20);
            }];
        }break;
        case 1:{
            // 只有图片
            self.titleLbl.hidden = YES;
            self.collectionView.hidden = NO;
            self.collectionView.scrollEnabled = NO;
            self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
            self.collectionView.collectionViewLayout = self.flowLayout;
            [_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self).offset(8);
                make.left.mas_equalTo(self).offset(16 - 5);
                make.right.mas_equalTo(self).offset(-16 + 5);
                make.bottom.mas_equalTo(self).offset(-8);
            }];
        }break;
        case 2:{
            // 只有图片
            self.titleLbl.hidden = YES;
            self.collectionView.hidden = NO;
            self.collectionView.scrollEnabled = YES;
            //滚动方向->水平
            self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
            self.collectionView.collectionViewLayout = self.flowLayout;
            [_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self).offset(0);
                make.left.mas_equalTo(self).offset(0);
                make.right.mas_equalTo(self).offset(0);
                make.bottom.mas_equalTo(self).offset(0);
            }];
        }break;
        case 3:{
            // 只有图片（用于小图片二级缩进）
            self.titleLbl.hidden = YES;
            self.collectionView.hidden = NO;
            self.collectionView.scrollEnabled = YES;
            self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
            self.collectionView.collectionViewLayout = self.flowLayout;
            [_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(8);
                make.left.mas_equalTo(32 - 5);
                make.right.mas_equalTo(-32 + 5);
                make.bottom.mas_equalTo(-8);
            }];
        }break;
        default:
            break;
    }
}

#pragma mark - Instial
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpBaseData];
        [self setUpUI];
    }
    return self;
}

-(void)setUpBaseData{
    
}

-(void)setUpUI{
    [self addSubview:self.titleLbl];
    [self addSubview:self.collectionView];
    [self lineV];
}
#pragma mark - Methods


#pragma mark - 布局
-(void)layoutSubviews{
    [super layoutSubviews];
}

#pragma mask - UICollectionViewDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    Weakself(self)
    BaseM *model = [self.dataArr objectAtIndex:indexPath.row];
    ZMInputImgCVCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZMInputImgCVCell class]) forIndexPath:indexPath];
    cell.model = model;
    cell.imageFreshBlock = ^(UIImage * _Nonnull image) {
//        NSMutableArray *tempArray = [NSMutableArray arrayWithArray:weakself.model.bsMarr1];
//        if (image != nil) {
//            if (indexPath.row < weakself.model.bsMarr1.count) {
//                [tempArray replaceObjectAtIndex:indexPath.row withObject:image];
//            }
//        }
//        weakself.model.bsMarr1 = tempArray;
    };
    cell.deleteBtnBlock = ^{
        !weakself.deleteBtnBlock?:weakself.deleteBtnBlock(self.model,indexPath.row);
    };
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //CGFloat top, left, bottom, right;
    UIEdgeInsets top={0,0,0,0};
    return top;
}

#pragma mark- 设置元素大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return self.model.bsSize;
}

/// 设置每个cell之间的行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0.0;
}

/// 设置列间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0.0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    BaseM *model = [self.dataArr objectAtIndex:indexPath.row];
    ZMInputImgCVCell *cell = (ZMInputImgCVCell *)[collectionView cellForItemAtIndexPath:indexPath];    !self.selItemBlock?:self.selItemBlock(model,cell.imgV,indexPath.row);
}

#pragma mark- Lazyload
-(UILabel *)titleLbl{
    if (!_titleLbl) {
        _titleLbl = [UILabel new];
        _titleLbl.font = kMediumFont(14);
        _titleLbl.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLbl;
}

-(UIView *)lineV{
    if (!_lineV) {
        _lineV = [UIView new];
        _lineV.hidden = YES;
        _lineV.backgroundColor = kHexColor(0xEEEEEE);
        [self addSubview:_lineV];
        [_lineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.bottom.mas_equalTo(0);
            make.height.mas_equalTo(1);
        }];
    }
    return _lineV;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;

        [self registCell];
    }
    return _collectionView;
}

-(void)registCell{
    [_collectionView registerClass:[ZMInputImgCVCell class] forCellWithReuseIdentifier:NSStringFromClass([ZMInputImgCVCell class])];
    //头部
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
    //尾部
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])]; //分割线
}

-(UICollectionViewFlowLayout *)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [UICollectionViewFlowLayout new];
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return _flowLayout;
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}

@end
